package com.atwisdom.star.core.bsl.functionClass.statistics.Impl;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.actionType.functionClass.StatisticsActionType;
import com.atwisdom.star.common.vo.*;
import com.atwisdom.star.core.bsl.functionClass.statistics.StatisticsModelBslService;
import com.atwisdom.star.core.dsl.functionClass.statistics.Impl.StatisticsModelDslServiceImpl;
import com.atwisdom.star.exception.pojo.BusinessException;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

@Service
public class StatisticsModelBslServiceImpl implements StatisticsModelBslService {

    @Autowired
    public void setStatisticsModelDslService(StatisticsModelDslServiceImpl statisticsModelDslService) {
        this.statisticsModelDslService = statisticsModelDslService;
    }

    private StatisticsModelDslServiceImpl statisticsModelDslService;


    /**
     * 服务工厂方法
     * @param actionDataList 参数
     * @return 参数
     * @throws BusinessException 异常
     */
    @Transactional
    public ResultInfo<?> responseFactory(List<ActionData<JSONObject>> actionDataList) throws BusinessException {
        ResultInfo<?> resultInfo = null;
        for (ActionData<JSONObject> actionData : actionDataList) {
            String action = actionData.getAction();
            StatisticsActionType actionType = StatisticsActionType.valueOf(action);
            String methodName = actionType.getMethodName();
            resultInfo = this.execMethod(methodName, actionData);
        }
        return resultInfo;
    }

    /**
     * 反射执行业务单元方法
     *
     * @param methodName 方法名
     * @param actionData 方法参数
     * @return 返回结果
     * @throws BusinessException 业务异常
     */
    public ResultInfo<?> execMethod(String methodName, ActionData<JSONObject> actionData) throws BusinessException {
        Class<? extends StatisticsModelBslServiceImpl> clz = this.getClass();
        try {
            Method method = clz.getMethod(methodName, ActionData.class);
            StatisticsModelBslServiceImpl service = (StatisticsModelBslServiceImpl) AopContext.currentProxy();
            return (ResultInfo<?>) method.invoke(service, actionData);
        } catch (NoSuchMethodException ex) {
            throw new BusinessException("999999", "没有找到该方法(" + methodName + ")", ex);
        } catch (IllegalAccessException ex) {
            throw new BusinessException("999999", "没有访问该方法权限(" + methodName + ")", ex);
        } catch (InvocationTargetException ex) {
            Throwable targetException = ex.getTargetException();
            if (targetException instanceof BusinessException) {
                BusinessException bex = (BusinessException) targetException;
                throw new BusinessException("999999", bex.getErrorMsg(), ex);
            } else {
                throw new BusinessException("999999", "出错了请重试：(" + methodName + ")---" + ex.getMessage(), ex);
            }
        }
    }

    /**
     * 查询全部摘要（通用）
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> queryAllInfo(ActionData<JSONObject> actionData) {
        List<JSONObject> jsonObjectList = this.statisticsModelDslService.queryAllInfo();
        return JsonResult.success(jsonObjectList);
    }

    /**
     * 分页查询全部摘要（通用）
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<PageInfo> queryAllInfoByPage(ActionData<JSONObject> actionData) {
        int pageNumber = actionData.getPageNumber();
        int pageSize = actionData.getPageSize();
        String orderBy = actionData.getOrderBy();
        PageInfo pageInfo = this.statisticsModelDslService.queryAllInfoByPage(pageNumber, pageSize, orderBy);
        return JsonResult.success(pageInfo);
    }

    /**
     * 查询全部数据（通用）
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> queryAll(ActionData<JSONObject> actionData) {
        List<JSONObject> jsonObjectList = this.statisticsModelDslService.queryAll();
        return JsonResult.success(jsonObjectList);
    }

    /**
     * 分页查询全部数据（通用）
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> listAllByPage(ActionData<JSONObject> actionData) {
        int pageNumber = actionData.getPageNumber();
        int pageSize = actionData.getPageSize();
        String orderBy = actionData.getOrderBy();
        PageInfo pageInfo = this.statisticsModelDslService.listAllByPage(pageNumber, pageSize, orderBy);
        return JsonResult.success(pageInfo);
    }

    /**
     * 按条件查询模型
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> query(ActionData<JSONObject> actionData) {
        String condition = actionData.getCondition();
        String className = actionData.getClassName();
        String orderBy = actionData.getOrderBy();
        List<JSONObject> query = this.statisticsModelDslService.query(condition, className, orderBy);
        return JsonResult.success(query);
    }


    /**
     * 根据类名查询容器属性
     * @param actionData 类名
     * @return 返回结果
     * @throws BusinessException 异常
     */
    public ResultInfo<?> queryHierProperties(ActionData<JSONObject> actionData){
        String className = actionData.getClassName();
        JSONObject jsonObject = this.statisticsModelDslService.queryHierProperties(className);
        return JsonResult.success(jsonObject);
    }

    /**
     * 按条件分页查询模型摘要
     * @param actionData 参数
     * @return 返回结果
     */
    public ResultInfo<?> queryInfoByPage(ActionData<JSONObject> actionData) {
        int pageNumber = actionData.getPageNumber();
        int pageSize = actionData.getPageSize();
        String condition = actionData.getCondition();
        String className = actionData.getClassName();
        String orderBy = actionData.getOrderBy();
        PageInfo pageInfo = this.statisticsModelDslService.queryInfoByPage(pageNumber, pageSize, condition, className, orderBy);
        return JsonResult.success(pageInfo);
    }

    /**
     * 按条件分页查询模型
     * @param actionData 参数
     * @return 返回结果
     */
    public ResultInfo<?> queryByPage(ActionData<JSONObject> actionData) {
        int pageNumber = actionData.getPageNumber();
        int pageSize = actionData.getPageSize();
        String condition = actionData.getCondition();
        String className = actionData.getClassName();
        String orderBy = actionData.getOrderBy();
        PageInfo pageInfo = this.statisticsModelDslService.queryByPage(pageNumber, pageSize, condition, className, orderBy);
        return JsonResult.success(pageInfo);
    }

    /**
     * 分页查询全部版本摘要（通用）
     *
     * @return 返回结果
     */
    @Override
    public ResultInfo<?> queryAllVersionInfoByPage(ActionData<JSONObject> actionData) {
        int pageNumber = actionData.getPageNumber();
        int pageSize = actionData.getPageSize();
        String orderBy = actionData.getOrderBy();
        PageInfo pageInfo = this.statisticsModelDslService.queryAllVersionInfoByPage(pageNumber, pageSize, orderBy);
        return JsonResult.success(pageInfo);
    }

    /**
     * 分页条件查询版本摘要（通用）
     * @return 返回结果
     */
    public ResultInfo<?> queryVersionInfoByPage(ActionData<JSONObject> actionData) {
        int pageNumber = actionData.getPageNumber();
        int pageSize = actionData.getPageSize();
        String condition = actionData.getCondition();
        String className = actionData.getClassName();
        String orderBy = actionData.getOrderBy();
        PageInfo pageInfo = this.statisticsModelDslService.queryVersionInfoByPage(pageNumber, pageSize, condition, className, orderBy);
        return JsonResult.success(pageInfo);
    }

    /**
     *按条件和版本查询（通用）
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> queryVersion(ActionData<JSONObject> actionData) {
        String condition = actionData.getCondition();
        String className = actionData.getClassName();
        BigDecimal version = actionData.getVersion();
        String orderBy = actionData.getOrderBy();
        List<JSONObject> jsonObjectList = this.statisticsModelDslService.queryVersion(condition, className, version, orderBy);
        return JsonResult.success(jsonObjectList);
    }

    /**
     * 设置版本有效
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> setPublish(ActionData<JSONObject> actionData) {
        String className = actionData.getClassName();
        BigDecimal version = actionData.getVersion();
        this.statisticsModelDslService.setPublish(className, version);
        return JsonResult.success(StatusCode.SUCCESS);
    }

    /**
     * 设置版本无效
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> setUnPublish(ActionData<JSONObject> actionData) {
        String className = actionData.getClassName();
        this.statisticsModelDslService.setUnPublish(className);
        return JsonResult.success(StatusCode.SUCCESS);
    }

    /**
     * 版本升级
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> upgrade(ActionData<JSONObject> actionData) {
        String className = actionData.getClassName();
        this.statisticsModelDslService.upgrade(className);
        return JsonResult.success(StatusCode.SUCCESS);
    }

    /**
     * 添加新模型
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> add(ActionData<JSONObject> actionData) {
        List<JSONObject> data = actionData.getData();
        JSONObject jsonObject = data.get(0);
        String type = jsonObject.getString("type");
        String className = jsonObject.getString("className");
        JSONObject addJsonObject = this.statisticsModelDslService.add(type, className);
        return JsonResult.success(addJsonObject);
    }

    /**
     * 拷贝模型
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> clone(ActionData<JSONObject> actionData) {
        String type = actionData.getType();
        String className = actionData.getClassName();
        String oldType = actionData.getOldType();
        String oldClassName = actionData.getOldClassName();
        JSONObject clone = this.statisticsModelDslService.clone(type, className, oldType, oldClassName);
        return JsonResult.success(clone);
    }

    /**
     * 更新模型类型和类名（配置阶段）
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> updateClassName(ActionData<JSONObject> actionData) {
        String type = actionData.getType();
        String className = actionData.getClassName();
        String oldType = actionData.getOldType();
        String oldClassName = actionData.getOldClassName();
        JSONObject jsonObject = this.statisticsModelDslService.updateClassName(type, className, oldType, oldClassName);
        return JsonResult.success(jsonObject);
    }

    /**
     * 更新模型属性字段
     * 更新，需要同步更新实例，同步更新历史数据
     * 1、className 和 自定字段
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> updateById(ActionData<JSONObject> actionData) {
        List<JSONObject> data = actionData.getData();
        JSONObject jsonObject = data.get(0);
        return JsonResult.success(this.statisticsModelDslService.updateById(jsonObject));
    }

    /**
     * 更新容器属性
     * @param actionData 更新参数
     * @return 返回更新后模型
     */
    public ResultInfo<?> updateHierProperties(ActionData<JSONObject> actionData){
        List<JSONObject> data = actionData.getData();
        JSONObject jsonObject = data.get(0);
        return JsonResult.success(this.statisticsModelDslService.updateHierProperties(jsonObject));
    }

    /**
     * 按ids删除模型
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> deleteByIds(ActionData<JSONObject> actionData) {
        List<Long> ids = actionData.getIds();
        this.statisticsModelDslService.deleteByIds(ids);
        return JsonResult.success(StatusCode.SUCCESS);
    }

    /**
     * 接className删除
     * 1、删除实例、2、删除历史 3、更新redis
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> deleteByClassName(ActionData<JSONObject> actionData) {
        String className = actionData.getClassName();
        this.statisticsModelDslService.deleteByClassName(className);
        return JsonResult.success(StatusCode.SUCCESS);
    }

    /**
     * 批量删除
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> deleteByClassNames(ActionData<JSONObject> actionData) {
        List<String> classNames = actionData.getClassNames();
        this.statisticsModelDslService.deleteByClassNames(classNames);
        return JsonResult.success(StatusCode.SUCCESS);
    }

    /**
     * 按Id 废除模型
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> repealByIds(ActionData<JSONObject> actionData) {
        List<Long> ids = actionData.getIds();
        this.statisticsModelDslService.repealByIds(ids);
        return JsonResult.success(StatusCode.SUCCESS);
    }

    /**
     * 批量废除
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<?> repealByClassNames(ActionData<JSONObject> actionData) {
        List<String> classNames = actionData.getClassNames();
        this.statisticsModelDslService.repealByClassNames(classNames);
        return JsonResult.success(StatusCode.SUCCESS);
    }
}
